/**  
 * 文 件 名: UserMgrAction.java  
 * 描    述：   
 * 版    权：Copyright (c)20012-2012 tongzhou All Rights Reserved.
 * 公    司: 同舟 
 * 作    者：彭定雄                     
 * 版    本: 1.0  
 * 创建时间: 2012-10-27
 *  
 * 修改历史：  
 * 时间                             作者                       版本                        描述  
 * ------------------------------------------------------------------  
 * 2012-10-27        彭定雄                1.0          1.0 Version  
 */         

/**
 * 
 */
package com.tongzhou.tongzhou.account.action;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;

import com.opensymphony.xwork2.ActionSupport;
import com.tongzhou.db.exception.DBException;
import com.tongzhou.tongzhou.account.dao.IAccountDAO;
import com.tongzhou.tongzhou.account.model.Account;
import com.tongzhou.tongzhou.account.model.SearchAccountCondition;
import com.tongzhou.tongzhou.initInfo.model.QuotType;
import com.tongzhou.tongzhou.initInfo.model.Region;
import com.tongzhou.tongzhou.quot.dao.IPaperDAO;
import com.tongzhou.tongzhou.quot.dao.IPaperTypeDAO;
import com.tongzhou.tongzhou.quot.model.Paper;
import com.tongzhou.tongzhou.quot.model.PaperType;
import com.tongzhou.util.FileUtil;
import com.tongzhou.util.Util;

public class AccountAction extends ActionSupport{
	private int id;
	private String company_name;//公司名称
	private String company_num;//公司编号
	private Account account;
	private List<Account> result=null;
	private SearchAccountCondition condition=null;
	
	private int pageSize ;
	private int currentPage ;
	private String orderBy ;
	private String orderByType ;
	private String judgment;
	
	private IAccountDAO dao=null;
	private List<Region> regions;
	private List<QuotType> quot_types;
	
	private File quoturl ;
	private String quoturlFileName ;
	private int account_id;//账户id
	private List<PaperType> paperTypes;
	List<Map<String, List<Paper>>> list = new ArrayList<Map<String,List<Paper>>>() ;
	
	private IPaperTypeDAO paperTypeDAO=null;
	private IPaperDAO paperDAO=null;
	/**
	 * 用户列表界面
	 *@author: 彭定雄
	 *@return 
	 *@throws DBException
	 */
	public String listAccount() throws DBException{
		checkSearchCondition();
		condition = new SearchAccountCondition();
		if (currentPage == 0){
			currentPage = 1;
		}
		condition.setCurrentPage(currentPage) ;
		condition.setCompany_name(company_name);
		condition.setCompany_num(company_num);
		condition.setOrderBy(orderBy);
		condition.setOrderByType(orderByType) ;
		condition.setPageSize(pageSize) ;
	    result = dao.listAccount(condition);
		return "listAccount";
	} 
	
	public String listAccountTemp() throws DBException{
		paperTypes=dao.getPaperTypeList(account_id);
		for(PaperType pt:paperTypes){
			List<Paper> papers=dao.getPaperList(pt.getId());
			Map<String,List<Paper>> map = new HashMap<String, List<Paper>>();
			map.put(pt.getPaper_type_name(),papers);
			list.add(map);
		}
		return "listAccountTemp";
	}
	
	
	public String toEditAccountTemp() throws DBException{
		return "editAccountTemp";
	}
	
	public String editAccountTemp() throws DBException{
		dao.deleteAccountQuot(account_id);
		uploadClxxFile();
		listAccount();
		return "listAccount";
	}
	
	
	/**
	 * 解析excel
	 */
	public void uploadClxxFile(){
		try {
			if(quoturl!=null){
				HSSFSheet sheet=FileUtil.readExcel(quoturl);
				uploadClxxApp(sheet);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ;
		}
	}
	
	public void uploadClxxApp(HSSFSheet sheet) throws Exception {
		int FirstRowNum = sheet.getFirstRowNum();
		int LastRowNum = sheet.getLastRowNum();
		try {
			int j=0;
			int k=0;
			TreeMap<Integer,String> map=new TreeMap<Integer,String>();
			HSSFRow row2 = sheet.getRow(FirstRowNum);
			if (row2 != null) {
				HSSFCell cell = row2.getCell(j);
				while(cell!=null){
					PaperType paperType=new PaperType();
					String paper_type_name = FileUtil.getHSSFCellValue(cell);
					if(paper_type_name.equals("")){
						k++;
						cell = row2.getCell(k);
						continue;
					}
					paperType.setPaper_type_name(paper_type_name);
					paperType.setAccount_id(account_id);
					String newId=Util.getNewID();
					paperType.setId(newId);
					paperTypeDAO.addPaperType(paperType);
					map.put(j, newId);
					j++;
					k++;
					cell = row2.getCell(k);
				}
			}
			for (int i = FirstRowNum+2; i <= LastRowNum; i++) {
				HSSFRow row = sheet.getRow(i);
				if (row != null) {
					for(int m=0;m<j;m++){
						Paper paper=new Paper();
						HSSFCell cell1 = row.getCell(m*2);
						if(cell1==null){
							continue;
						}
						
						String paper_name = FileUtil.getHSSFCellValue(cell1);
						if(paper_name==null||paper_name.equals("")){
							continue;
						}
						paper.setPaper_name(paper_name);
						
						HSSFCell cell2 = row.getCell(m*2+1);
						String price = FileUtil.getHSSFCellValue(cell2);
						paper.setPrice(price);
						
						paper.setKeng("1");
						paper.setPaper_type_id(map.get(m));
						
						paperDAO.addPaper(paper);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("插入数据时发生异常");
		}
	}
	
	
	/**
	 * 设置列表显示格式
	 *@author: 彭定雄
	 */
	private void checkSearchCondition(){
		if(currentPage <=0){
			currentPage = 1 ;
		}
		
		if(pageSize<=0){
			pageSize = 10 ;
		}
		
		if(orderBy==null||"".equals(orderBy)){
			orderBy = "id" ;
		}
		
		if(orderByType == null || "".equals(orderByType)){
			orderByType = "desc" ;
		}
	}
	
	/**
	 * 新增用户
	 *@author: 彭定雄
	 *@return
	 *@throws DBException
	 */
	public String toAddAccount() throws DBException{
		regions=dao.getRegions();
		quot_types=dao.getQuotTypes();
		return "addAccount";
	}
	
	public String checkAccountByCode() throws DBException{
		account=new Account();
		account.setId(id);
		account.setCompany_num(company_num);
		result=dao.checkAccountByCode(account);
		if(result.size()>0){
			judgment="0";
		}else{
			judgment="1";
		}
		return SUCCESS;
	}
	
	public String addAccount() throws DBException{
		dao.addAccount(account);
		listAccount();
		return "listAccount";
	}
	
	public String deleteAccount() throws DBException{
//		dao=new UserMgrDAO();
		dao.deleteAccount(id);
		listAccount();
		return "listAccount";
	}
	
	public String toEditAccount() throws DBException{
		regions=dao.getRegions();
		quot_types=dao.getQuotTypes();
		account=dao.getAccountById(id);
		return "editAccount";
	}
	
	public String toDetailAccount() throws DBException{
		regions=dao.getRegions();
		quot_types=dao.getQuotTypes();
		account=dao.getAccountById(id);
		return "detailAccount";
	}
	
	public String editAccount() throws DBException{
		dao.updateAccount(account);
		listAccount();
		return "listAccount";
	}

	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}

	/**
	 * @return the company_name
	 */
	public String getCompany_name() {
		return company_name;
	}


	/**
	 * @param company_name the company_name to set
	 */
	public void setCompany_name(String company_name) {
		this.company_name = company_name;
	}


	/**
	 * @return the company_num
	 */
	public String getCompany_num() {
		return company_num;
	}


	/**
	 * @param company_num the company_num to set
	 */
	public void setCompany_num(String company_num) {
		this.company_num = company_num;
	}


	/**
	 * @return the account
	 */
	public Account getAccount() {
		return account;
	}


	/**
	 * @param account the account to set
	 */
	public void setAccount(Account account) {
		this.account = account;
	}


	/**
	 * @return the result
	 */
	public List<Account> getResult() {
		return result;
	}


	/**
	 * @param result the result to set
	 */
	public void setResult(List<Account> result) {
		this.result = result;
	}


	/**
	 * @return the condition
	 */
	public SearchAccountCondition getCondition() {
		return condition;
	}


	/**
	 * @param condition the condition to set
	 */
	public void setCondition(SearchAccountCondition condition) {
		this.condition = condition;
	}


	/**
	 * @return the pageSize
	 */
	public int getPageSize() {
		return pageSize;
	}


	/**
	 * @param pageSize the pageSize to set
	 */
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}


	/**
	 * @return the currentPage
	 */
	public int getCurrentPage() {
		return currentPage;
	}


	/**
	 * @param currentPage the currentPage to set
	 */
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}


	/**
	 * @return the orderBy
	 */
	public String getOrderBy() {
		return orderBy;
	}


	/**
	 * @param orderBy the orderBy to set
	 */
	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}


	/**
	 * @return the orderByType
	 */
	public String getOrderByType() {
		return orderByType;
	}


	/**
	 * @param orderByType the orderByType to set
	 */
	public void setOrderByType(String orderByType) {
		this.orderByType = orderByType;
	}


	/**
	 * @return the dao
	 */
	public IAccountDAO getDao() {
		return dao;
	}


	/**
	 * @param dao the dao to set
	 */
	public void setDao(IAccountDAO dao) {
		this.dao = dao;
	}


	public String getJudgment() {
		return judgment;
	}


	public void setJudgment(String judgment) {
		this.judgment = judgment;
	}


	public List<Region> getRegions() {
		return regions;
	}


	public void setRegions(List<Region> regions) {
		this.regions = regions;
	}


	public List<QuotType> getQuot_types() {
		return quot_types;
	}


	public void setQuot_types(List<QuotType> quotTypes) {
		quot_types = quotTypes;
	}

	public File getQuoturl() {
		return quoturl;
	}

	public void setQuoturl(File quoturl) {
		this.quoturl = quoturl;
	}

	public String getQuoturlFileName() {
		return quoturlFileName;
	}

	public void setQuoturlFileName(String quoturlFileName) {
		this.quoturlFileName = quoturlFileName;
	}

	public int getAccount_id() {
		return account_id;
	}

	public void setAccount_id(int accountId) {
		account_id = accountId;
	}

	public List<PaperType> getPaperTypes() {
		return paperTypes;
	}

	public void setPaperTypes(List<PaperType> paperTypes) {
		this.paperTypes = paperTypes;
	}

	public List<Map<String, List<Paper>>> getList() {
		return list;
	}

	public void setList(List<Map<String, List<Paper>>> list) {
		this.list = list;
	}

	public IPaperTypeDAO getPaperTypeDAO() {
		return paperTypeDAO;
	}

	public void setPaperTypeDAO(IPaperTypeDAO paperTypeDAO) {
		this.paperTypeDAO = paperTypeDAO;
	}

	public IPaperDAO getPaperDAO() {
		return paperDAO;
	}

	public void setPaperDAO(IPaperDAO paperDAO) {
		this.paperDAO = paperDAO;
	}
}
